﻿using CurrentCostLibrary;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using Communergy.Common;
namespace CurrentCostTest
{
    
    
    /// <summary>
    ///This is a test class for ReadingParserTest and is intended
    ///to contain all ReadingParserTest Unit Tests
    ///</summary>
    [TestClass()]
    public class ReadingParserTest_Monthly
    {


        private TestContext testContextInstance;
        

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion



        [TestMethod()]
        public void CurrentCost_GetCurrentCostHistoryMonthly_ReturnsCorrectSensorId()
        {
            DateTime readingDate = CurrentCostSampleReadings.CC_READING_DATE;
            string inputFromCurrentCost = CurrentCostSampleReadings.CC_MONTHLY_XML;
            int sensorIdToRead = CurrentCostSampleReadings.CC_EXPECTED_SENSORID;

            var actual = ReadingParser.GetCurrentCostHistory(readingDate, inputFromCurrentCost, sensorIdToRead);
            Assert.IsNotNull(actual);
            Assert.AreEqual(CurrentCostSampleReadings.CC_EXPECTED_SENSORID, actual.SensorId);
        }

        [TestMethod()]
        public void CurrentCost_GetCurrentCostHistoryMonthly_ReturnsCorrectReadingDateTime()
        {
            DateTime readingDate = CurrentCostSampleReadings.CC_READING_DATE;
            string inputFromCurrentCost = CurrentCostSampleReadings.CC_MONTHLY_XML;
            int sensorIdToRead = CurrentCostSampleReadings.CC_EXPECTED_SENSORID;

            var actual = ReadingParser.GetCurrentCostHistory(readingDate, inputFromCurrentCost, sensorIdToRead);
            Assert.IsNotNull(actual);
            var expectedReadingDateTime = new DateTime(2009, 12, 7, 7, 19, 20);
            Assert.AreEqual(expectedReadingDateTime, actual.ReadingDateTime);
        }

        [TestMethod()]
        public void CurrentCost_GetCurrentCostHistoryMonthly_ReturnsCorrectMonthlyCount()
        {
            DateTime readingDate = CurrentCostSampleReadings.CC_READING_DATE;
            string inputFromCurrentCost = CurrentCostSampleReadings.CC_MONTHLY_XML;
            int sensorIdToRead = CurrentCostSampleReadings.CC_EXPECTED_SENSORID;

            var actual = ReadingParser.GetCurrentCostHistory(readingDate, inputFromCurrentCost, sensorIdToRead);
            Assert.IsNotNull(actual);
            Assert.IsNotNull(actual.MonthlyReadings);
            Assert.AreEqual(2, actual.MonthlyReadings.Count);
        }

        [TestMethod()]
        public void CurrentCost_GetCurrentCostHistoryMonthly_ReturnsCorrectMonthlyValues()
        {
            DateTime readingDate = CurrentCostSampleReadings.CC_READING_DATE;
            DateTime readingMonth= new DateTime(readingDate.Year, readingDate.Month, 1);
            string inputFromCurrentCost = CurrentCostSampleReadings.CC_MONTHLY_XML;
            int sensorIdToRead = CurrentCostSampleReadings.CC_EXPECTED_SENSORID;

            var actual = ReadingParser.GetCurrentCostHistory(readingDate, inputFromCurrentCost, sensorIdToRead);
            Assert.IsNotNull(actual);
            Assert.IsNotNull(actual.MonthlyReadings);
            Assert.AreEqual(2, actual.MonthlyReadings.Count);

            var month1 = actual.MonthlyReadings[1];

            Assert.AreEqual(readingMonth.AddMonths(-1), month1.ReadingDateTime.Date);

            TimeSpan interval = readingDate.MonthTimeSpan();
            decimal expectedKWh = 331.0M ;

            Assert.AreEqual(expectedKWh, month1.EnergyInformationFromData().KWh);

            var month2 = actual.MonthlyReadings[0];
            Assert.AreEqual(readingMonth.AddMonths(-2), month2.ReadingDateTime.Date);

            interval = month2.ReadingDateTime.MonthTimeSpan();
            expectedKWh = 247.750M ;

            Assert.AreEqual(expectedKWh, month2.EnergyInformationFromData().KWh);

        }

        [TestMethod()]
        public void CurrentCost_GetCurrentCostHistoryMonthly_ReturnsCorrectEmptySensor()
        {
            DateTime readingDate = CurrentCostSampleReadings.CC_READING_DATE;
            string inputFromCurrentCost = CurrentCostSampleReadings.CC_MONTHLY_XML;
            int sensorIdToRead = 1;

            var actual = ReadingParser.GetCurrentCostHistory(readingDate, inputFromCurrentCost, sensorIdToRead);
            Assert.IsNotNull(actual);
            Assert.IsNotNull(actual.MonthlyReadings);
            Assert.AreEqual(2, actual.MonthlyReadings.Count);
            foreach (var reading in actual.MonthlyReadings)
            {
                Assert.AreEqual((decimal)0.0, reading.EnergyInformationFromData().KWh);
            }
        }

        [TestMethod()]
        public void CurrentCost_GetCurrentCostHistoryMonthly_ReturnsCorrectNoOtherReadings()
        {
            DateTime readingDate = CurrentCostSampleReadings.CC_READING_DATE;
            string inputFromCurrentCost = CurrentCostSampleReadings.CC_MONTHLY_XML;
            int sensorIdToRead = CurrentCostSampleReadings.CC_EXPECTED_SENSORID;

            var actual = ReadingParser.GetCurrentCostHistory(readingDate, inputFromCurrentCost, sensorIdToRead);
            Assert.AreEqual(0, actual.HourlyReadings.Count);
            Assert.AreEqual(0, actual.DailyReadings.Count);
        }
    }
}
